/*
 * @Description: 元数据添加、编辑通用表单
 * @Author: Rfan
 * @Date: 2022-10-26 11:37:59
 * @LastEditTime: 2022-10-26 15:24:30
 */

import { FC, useEffect, useState } from 'react';
import { Form, Input, Radio, Select, Switch } from 'antd';
import { useMetadataContext } from '@/view/system/Metadata/contexts/MetaContext';
import { fetchDicList } from '@/api/rulesettings/dictionary';

const { Option } = Select;
interface IMetadataFormProps {
  visible: boolean;
  dicType: number;
  setDicType: any;
  aliasList: any[];
}

const MetadataForm: FC<IMetadataFormProps> = (props) => {
  const { visible, dicType, setDicType, aliasList } = props;
  const { selectAT, typeList, kindList } = useMetadataContext();

  // 字典表列表
  const [dicList, setDicList] = useState([]);

  useEffect(() => {
    if (visible) {
      getDicList();
    }
  }, [visible, dicType]);

  const getDicList = () => {
    let archiveTypeId = 0;
    if (dicType === 2) {
      archiveTypeId = selectAT.id;
    }
    fetchDicList({ archiveTypeId }).then((res) => {
      const { data } = res;
      setDicList(data);
    });
  };

  return (
    <>
      <Form.Item name="delete" label="状态">
        <Radio.Group>
          <Radio value={0}>正常</Radio>
          <Radio value={1}>停用</Radio>
        </Radio.Group>
      </Form.Item>
      <Form.Item
        name="field_desc"
        label="中文名称"
        extra="展示的名称，必填项，不可重复"
        rules={[{ required: true }]}
      >
        <Input placeholder="请输入显示用的标题" allowClear />
      </Form.Item>
      <Form.Item
        name="field_name"
        label="英文名称"
        extra="数据库标识，必填项、不可重复"
        rules={[{ required: true }]}
      >
        <Input placeholder="请输入数据库中的标志" allowClear />
      </Form.Item>
      <Form.Item name="business_concept_id" label="别名">
        <Select placeholder="请选择别名">
          {aliasList.map((item) => (
            <Option key={item.id} value={item.id}>
              {item.cName}
            </Option>
          ))}
        </Select>
      </Form.Item>
      <Form.Item name="field_type" label="数据类型">
        <Select>
          {typeList.map((item: any) => (
            <Option key={item.id} value={item.id}>
              {item.name}
            </Option>
          ))}
        </Select>
      </Form.Item>
      <Form.Item name="field_kind" label="字段种类">
        <Select>
          {kindList.map((item: any) => (
            <Option key={item.id} value={item.id}>
              {item.name}
            </Option>
          ))}
        </Select>
      </Form.Item>
      <Form.Item
        name="length"
        label="数据长度"
        extra="存放数据最大长度，必须输入，必须是整数"
        rules={[{ required: true }]}
      >
        <Input placeholder="请输入数据长度" allowClear />
      </Form.Item>
      <Form.Item label="列表长度" extra="列表默认长度" rules={[{ required: true }]}>
        <Input placeholder="请输入列表长度" allowClear disabled />
      </Form.Item>
      <Form.Item
        name="default_value"
        extra="输入‘{’后可选择系统 参数作为默认值的组成部分"
        label="默认值"
      >
        <Input allowClear />
      </Form.Item>
      <Form.Item name="allowed_dup" valuePropName="checked" label="允许重复">
        <Switch />
      </Form.Item>
      <Form.Item name="allowed_null" valuePropName="checked" label="允许空">
        <Switch defaultChecked />
      </Form.Item>
      {/* 暂时不展示，后期决定是否删除 */}
      {/* <Form.Item label="扩展字段" wrapperCol={{ span: 18 }}> */}
      {/*  <Checkbox.Group disabled> */}
      {/*    <Space wrap> */}
      {/*      <Checkbox>系统字段</Checkbox> */}
      {/*      <Checkbox>利用可见</Checkbox> */}
      {/*      <Checkbox>全文检索</Checkbox> */}
      {/*      <Checkbox>关联检索</Checkbox> */}
      {/*      <Checkbox>后台列表</Checkbox> */}
      {/*      <Checkbox>后台导出</Checkbox> */}
      {/*      <Checkbox>必填字段</Checkbox> */}
      {/*      <Checkbox>允许编辑</Checkbox> */}
      {/*      <Checkbox>批量修改</Checkbox> */}
      {/*      <Checkbox>追加复制</Checkbox> */}
      {/*      <Checkbox>继承上级</Checkbox> */}
      {/*    </Space> */}
      {/*  </Checkbox.Group> */}
      {/* </Form.Item> */}
      <Form.Item label="数据字典" extra="该字段值创建界面将有下拉列表选择字典数值。">
        <Form.Item style={{ marginBottom: '4px' }}>
          <Radio.Group
            value={dicType}
            onChange={(evt) => {
              setDicType(evt.target.value);
            }}
          >
            <Radio value={1}>公共字典</Radio>
            <Radio value={2}>档案类型字典</Radio>
          </Radio.Group>
        </Form.Item>
        <Form.Item name="dict_type_id" noStyle>
          <Select placeholder="请选择数据字典" allowClear>
            {dicList.map((dic) => (
              <Option key={dic.id} value={dic.id}>
                {dic.name}
              </Option>
            ))}
          </Select>
        </Form.Item>
      </Form.Item>
    </>
  );
};

export default MetadataForm;
